package com.enbatis.mybatisplugs.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

@Component
public class InitPool {
    public static final List<Connection> listXRayAnalyseUtil=new ArrayList();
    @Bean
    public boolean initList(){
        try {
            for (int i = 0; i < 5; i++) {

                Class.forName("com.mysql.jdbc.Driver");

                String dbUrl="jdbc:mysql://106.13.210.248:3306/test?useUnicode=true&characterEncoding=utf8";
                Connection conn  = DriverManager.getConnection(dbUrl,"root","wwd@!199117");
                listXRayAnalyseUtil.add(conn);
            }

        }catch (Exception e){
          e.getMessage();
        }
        return  true;
    }
    @Bean
    public List<Connection> getConnectionList(){
        return  listXRayAnalyseUtil;
    }

    @Bean
    public Connection getConnection(){
        if (listXRayAnalyseUtil.size()>0){
            Connection connection=   listXRayAnalyseUtil.get(0);
            listXRayAnalyseUtil.remove(0);
            return  connection;
        }
        return null;

    }
}
